package com.search.controller;

import com.search.analysis.FactoryAnalysis;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

@Controller
public class IndexController implements Initializable {

    @FXML
    private TextField searchText;

    @FXML
    private ListView<Pane> searchList;


    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @Autowired
    private FactoryAnalysis factoryAnalysis;

    private AtomicBoolean isSearch = new AtomicBoolean(true);

    @FXML
    void searchClick(ActionEvent event) {
        //如果上次的搜索没有结束,那么就取消这次事件
        if (!isSearch.get()) {
            if (event != null)
                event.consume();//取消这次事件(已消费事件)
            return;
        }
        isSearch.compareAndSet(isSearch.get(), false);//关闭搜索
        ObservableList<Pane> items = searchList.getItems();
        //取消上次的搜索数据
        items.clear();
        //禁用searchText
        searchText.setEditable(false);
        //背景颜色改为灰色
        searchText.setStyle("-fx-background-color: #e6e6e6");
        //获取搜索框的内容
        String text = searchText.getText();
        if (text == null || "".equals(text.trim())) {
            return;
        }
        ////将搜索的时间轴放入全局变量中,方便下次取消
        LinkedBlockingQueue<Pane> panes = factoryAnalysis.searchBean(text);
        Timeline timeline = new Timeline(new KeyFrame(Duration.millis(1000), event1 -> {
            //持续搜索,直到队列中没有数据
            Pane take1 = panes.poll();
            while (take1 != null) {
                items.add(take1);
                take1 = panes.poll();
            }
        }));
        timeline.setCycleCount(Timeline.INDEFINITE);//循环执行   或者设置次数
        timeline.play();
        new Thread(() -> {
            int size1 = items.size();
            int size2 = items.size();
            try {
                while (size1 == size2) {//判断是否开始有数据
                    size1 = items.size();
                    Thread.sleep(1000);
                    size2 = items.size();
                }
                //如果有数据了那么就开始检测是否有新数据,如果在一段时间内没有新数据,那么就认为搜索结束
                while (size1 != size2) {
                    size1 = items.size();
                    Thread.sleep(2000);
                    size2 = items.size();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            searchText.setEditable(true);//开启搜索框
            //取消背景颜色
            searchText.setStyle("-fx-background-color: #ffffff");
            timeline.stop();//停止时间轴
            isSearch.compareAndSet(isSearch.get(), true);//开启搜索
        }).start();


    }

    @FXML
    void searchEnter(KeyEvent event) {
        KeyCode code = event.getCode();
        if (code == KeyCode.ENTER) {//回车键
            searchClick(null);
        }
    }
}
